﻿Imports VN.INS.BL.BC
Imports System.Data
Imports System.Web
Imports System.IO

Partial Class Devida_ReporteFormato4
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then
            ListaPPER()
            Listaperiodo()
            btnGenerar.Attributes.Add("onclick", "javascript:document.getElementById('" + btnGenerar.ClientID + "').disabled=true;" + Me.GetPostBackEventReference(btnGenerar))
        End If
    End Sub

    Public Sub cargar_grilla(ByVal mes As String, ByVal año As String, ByVal pper As String)
        Dim ObjconsolidadoBC As consolidadoBC
        Dim ObjData As DataTable
        Dim ObjData1 As DataTable
        'Cargamos detalle de grilla principal
        ObjconsolidadoBC = New consolidadoBC
        ObjData = ObjconsolidadoBC.cargar_grilla(mes, año, ddlPPER.SelectedValue)
        ViewState("Data2") = ObjData
        ' 'Cargamos el tipo de transeferencia
        ObjData1 = ObjconsolidadoBC.ListarTipoAsignacion_PPER(Session("Año"), ddlPPER.SelectedValue)
        ViewState("Data1") = ObjData1
        gvconsolidado.DataSource = ObjData1
        gvconsolidado.DataBind()
    End Sub

    Public Sub cargar_grilla2()
        'Cargamos Datos de la segunda grilla
        Dim ObjconsolidadoBC As consolidadoBC
        Dim ObjData As DataTable
        Dim ObjData1 As DataTable
        Dim ObjData2 As DataTable
        Dim ObjData3 As DataTable
        Dim ObjData4 As DataTable
        Dim objData5 As DataTable
        ObjconsolidadoBC = New consolidadoBC
        ObjData = ObjconsolidadoBC.cargar_grilla2
        ObjData1 = ObjconsolidadoBC.tipo_trans2
        ObjData2 = ObjconsolidadoBC.tipo_PROY
        ObjData3 = ObjconsolidadoBC.tipo_ACTV
        ObjData4 = ObjconsolidadoBC.tipo_ACC
        objData5 = ObjconsolidadoBC.CargarTotalTipo()

        ViewState("data2") = ObjData
        ViewState("tipo_PROY") = ObjData2
        ViewState("tipo_ACTV") = ObjData3
        ViewState("tipo_ACC") = ObjData4
        ViewState("TotalTipo") = objData5
        If Not (ObjData1 Is Nothing) Then
            If ObjData1.Rows.Count > 0 Then
                gvFormato5.DataSource = ObjData1
                gvFormato5.DataBind()
            End If
        End If
    End Sub

    Public Sub cargar_grilla_coment(ByVal cod As String)
        Dim ObjconsolidadoBC As consolidadoBC
        Dim ObjData As DataTable
        'Cargamos comentarios de el informe dado
        ObjconsolidadoBC = New consolidadoBC
        ObjData = ObjconsolidadoBC.cargar_grilla_coment(cod)

        If ObjData.Rows.Count = 0 Then 'Si no hay comentarios cargamos textbox vacio
            Dim dtcomentario As New DataTable
            dtcomentario.Columns.Add("DESCRIPCION")
            dtcomentario.Columns.Add("COMENT")
            Dim rowc As DataRow = dtcomentario.NewRow()
            rowc("DESCRIPCION") = ""
            rowc("COMENT") = ""
            dtcomentario.Rows.Add(rowc)
            Session("dtcomentario") = dtcomentario
            'gv_comentario.DataSource = dtcomentario
            'gv_comentario.DataBind()
        Else 'Si hay datos entonces se muestran
            Session("dtcomentario") = ObjData
            'gv_comentario.DataSource = ObjData
            'gv_comentario.DataBind()
        End If

    End Sub

    Public Sub cargar_grilla_acc(ByVal cod As String)
        Dim ObjconsolidadoBC As consolidadoBC
        Dim ObjData As DataTable
        ObjconsolidadoBC = New consolidadoBC
        'Cargamos acciones de el informe dado
        ObjData = ObjconsolidadoBC.cargar_grilla_acc(cod)
        If ObjData.Rows.Count = 0 Then 'Si no hay acciones cargamos textbox vacio
            Dim dtaccion As New DataTable
            dtaccion.Columns.Add("DESCRIPCION")
            dtaccion.Columns.Add("COMENT")
            Dim rowA As DataRow = dtaccion.NewRow()
            rowA("DESCRIPCION") = ""
            rowA("COMENT") = ""
            dtaccion.Rows.Add(rowA)
            Session("dtaccion") = dtaccion
            'gv_acciones.DataSource = dtaccion
            'gv_acciones.DataBind()
        Else 'Si hay datos entonces se muestran
            Session("dtaccion") = ObjData
            'gv_acciones.DataSource = ObjData
            'gv_acciones.DataBind()
        End If
    End Sub

    Public Sub cargar_grilla_prob(ByVal cod As String)
        Dim ObjconsolidadoBC As consolidadoBC
        Dim ObjData As DataTable
        ObjconsolidadoBC = New consolidadoBC
        'Cargamos problemas de el informe dado
        ObjData = ObjconsolidadoBC.cargar_grilla_prob(cod)
        If ObjData.Rows.Count = 0 Then 'Si no hay problemas cargamos textbox vacio
            Dim dtproblema As New DataTable
            dtproblema.Columns.Add("DESCRIPCION")
            dtproblema.Columns.Add("COMENT")
            Dim rowp As DataRow = dtproblema.NewRow()
            rowp("DESCRIPCION") = ""
            rowp("COMENT") = ""
            dtproblema.Rows.Add(rowp)
            Session("dtproblema") = dtproblema
            'gv_problemas.DataSource = dtproblema
            'gv_problemas.DataBind()
        Else 'Si hay datos entonces se muestran
            Session("dtproblema") = ObjData
            'gv_problemas.DataSource = ObjData
            'gv_problemas.DataBind()
        End If
    End Sub

    Public Sub ListaPPER()
        Dim objPPER As PPERBC
        Dim objData As DataTable
        objPPER = New PPERBC
        objData = New DataTable
        objData = objPPER.ListaPPER("")
        ddlPPER.DataSource = objData
        ddlPPER.DataTextField = "DESCRIPCION"
        ddlPPER.DataValueField = "PPER"
        ddlPPER.DataBind()
    End Sub

    Private Sub Listaperiodo()
        Dim objBC As GeneralBC
        Dim objDt As DataTable
        Try
            objBC = New GeneralBC
            objDt = New DataTable
            objDt = objBC.ListaMes(Session("Año"))
            ddlPeriodo.DataSource = objDt
            ddlPeriodo.DataTextField = "DESCRIPCION"
            ddlPeriodo.DataValueField = "PERIODO"
            ddlPeriodo.DataBind()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnGenerar_Click(sender As Object, e As ImageClickEventArgs) Handles btnGenerar.Click
        'System.Threading.Thread.Sleep(5000)

        'Cargamos grilla Comentario con textbox vacio
        Dim dtcomentario As New DataTable
        dtcomentario.Columns.Add("DESCRIPCION")
        dtcomentario.Columns.Add("COMENT")
        Dim rowc As DataRow = dtcomentario.NewRow()
        rowc("DESCRIPCION") = ""
        rowc("COMENT") = ""
        dtcomentario.Rows.Add(rowc)
        Session("dtcomentario") = dtcomentario
        'Cargamos grilla Accion con textbox vacio
        Dim dtaccion As New DataTable
        dtaccion.Columns.Add("DESCRIPCION")
        dtaccion.Columns.Add("COMENT")
        Dim rowA As DataRow = dtaccion.NewRow()
        rowA("DESCRIPCION") = ""
        rowA("COMENT") = ""
        dtaccion.Rows.Add(rowA)
        Session("dtaccion") = dtaccion
        'Cargamos grilla Problemas con textbox vacio
        Dim dtproblema As New DataTable
        dtproblema.Columns.Add("DESCRIPCION")
        dtproblema.Columns.Add("COMENT")
        Dim rowp As DataRow = dtproblema.NewRow()
        rowp("DESCRIPCION") = ""
        rowp("COMENT") = ""
        dtproblema.Rows.Add(rowp)
        Session("dtproblema") = dtproblema

        'Cargamos grilla de primera pestaña
        cargar_grilla(ddlPeriodo.SelectedValue.Substring(4, 2), ddlPeriodo.SelectedValue.Substring(0, 4), Session("PPER").ToString)
        'Cargamos grilla de segunda pestaña
        cargar_grilla2()
        divReporte.Style("display") = "block"
    End Sub

    Public Sub RDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then ' Por cada fila           
            e.Row.BackColor = Drawing.Color.White
        End If
    End Sub

    Public Sub RDB2(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim hfCOD As HiddenField = CType(e.Row.FindControl("hfCOD"), HiddenField)
            Dim Hftip As HiddenField = CType(e.Row.FindControl("Hftip"), HiddenField)
            Dim lblProducto As Label = CType(e.Row.FindControl("lblPPER2"), Label)
            Dim A As String
            Dim DATA As New DataTable
            If Hftip.Value = "1" Then
                DATA = ViewState("data2")
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(255, 255, 218)
                A = "P"
            End If
            If Hftip.Value = "2" Then
                DATA = ViewState("data2")
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(255, 255, 218)
                A = "A"
            End If
            If Hftip.Value = "3" Then
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(255, 255, 218)
                DATA = ViewState("data2")
                A = "AC"
            End If
            With e.Row.Cells(0)
                .ForeColor = Drawing.Color.FromArgb(0, 0, 0)
                .HorizontalAlign = HorizontalAlign.Center
                .Text.ToUpper()
            End With
            ViewState("Producto") = lblProducto.Text
            Dim vst As DataView
            Dim Hftip2 As String = Hftip.Value.ToString
            vst = New DataView(DATA, "TIPO_PROYECTO_ACTIVIDAD='" + Hftip2 + "' AND TIPO_PROYECTO_DES = '" + hfCOD.Value.ToString + "'", "", DataViewRowState.CurrentRows)
            ViewState("TOTAL") = 0
            ViewState("MONTO_PROGRAMADO") = 0
            ViewState("MONTO_TRANSFERIDO") = 0
            ViewState("MONTO_GIRADO") = 0

            Dim dt As New DataTable
            dt = vst.ToTable

            For i As Int16 = 0 To dt.Rows.Count - 1
                ViewState("TOTAL") = ViewState("TOTAL") + dt.Rows(i)("Total")
                ViewState("MONTO_PROGRAMADO") = ViewState("MONTO_PROGRAMADO") + dt.Rows(i)("MONTO_PROGRAMADO")
                ViewState("MONTO_TRANSFERIDO") = ViewState("MONTO_TRANSFERIDO") + dt.Rows(i)("MONTO_TRANSFERIDO")
                ViewState("MONTO_GIRADO") = ViewState("MONTO_GIRADO") + dt.Rows(i)("MONTO_GIRADO")
            Next
            Dim gdv As GridView = e.Row.Cells(1).FindControl("gvDetalle3")
            gdv.DataSource = vst
            gdv.DataBind()
        ElseIf e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(0).ColumnSpan = 2
            e.Row.Cells(0).ForeColor = Drawing.Color.Black
            e.Row.Cells(1).Visible = False
            Dim lblTitulo As Label = CType(e.Row.FindControl("lblTitulo"), Label)
            Dim lblTotal As Label = CType(e.Row.FindControl("lblTotal"), Label)
            Dim lblProgramado As Label = CType(e.Row.FindControl("lblProgramado"), Label)
            Dim lblTransferido As Label = CType(e.Row.FindControl("lblTransferido"), Label)
            Dim lblGirado As Label = CType(e.Row.FindControl("lblGirado"), Label)
            Dim lblPorcentaje As Label = CType(e.Row.FindControl("lblPorcentaje"), Label)
            Dim vst As DataView
            vst = New DataView(ViewState("TotalTipo"), "TIPO_PROYECTO_ACTIVIDAD='" + ViewState("TIPO").ToString + "'", "", DataViewRowState.CurrentRows)
            lblTotal.Text = Convert.ToDecimal(vst.ToTable().Rows(0).Item("TOTAL")).ToString("#,##0.00")
            lblProgramado.Text = Convert.ToDecimal(vst.ToTable().Rows(0).Item("MONTO_PROGRAMADO")).ToString("#,##0.00")
            lblTransferido.Text = Convert.ToDecimal(vst.ToTable().Rows(0).Item("MONTO_TRANSFERIDO")).ToString("#,##0.00")
            lblGirado.Text = Convert.ToDecimal(vst.ToTable().Rows(0).Item("MONTO_GIRADO")).ToString("#,##0.00")
            lblPorcentaje.Text = Convert.ToDecimal(vst.ToTable().Rows(0).Item("PORCENT")).ToString("0 %")
            Select Case ViewState("TIPO")
                Case "1"
                    lblTitulo.Text = "TOTAL PROYECTOS"
                Case "2"
                    lblTitulo.Text = "TOTAL PRODUCTOS"
                Case "3"
                    lblTitulo.Text = "TOTAL ACCIONES COMUNES"
            End Select
        End If
    End Sub

    Private _total, _prog, _transf, _girado As Decimal
    Public Sub RDB3(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        _total = ViewState("TOTAL")
        _prog = ViewState("MONTO_PROGRAMADO")
        _transf = ViewState("MONTO_TRANSFERIDO")
        _girado = ViewState("MONTO_GIRADO")
        If e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(0).ColumnSpan = 2
            e.Row.Cells(8).Visible = False
            e.Row.Cells(0).Text = "Total " + ViewState("Producto").ToString
            For i As Int16 = 0 To 7
                If i = 0 Then
                    e.Row.Cells(i).BackColor = Drawing.Color.FromArgb(200, 200, 200)
                Else
                    e.Row.Cells(i).BackColor = Drawing.Color.FromArgb(255, 255, 255)
                    e.Row.Cells(i).Style("text-align") = "Right"
                    e.Row.Cells(i).Style("font-size") = "9px"
                    e.Row.Cells(i).Style(" font-family") = "Arial"
                    e.Row.Cells(i).Font.Bold = True
                End If
                e.Row.Cells(i).ForeColor = Drawing.Color.FromArgb(0, 0, 0)
                e.Row.Cells(i).Font.Bold = False
                e.Row.Cells(i).BorderColor = Drawing.Color.FromArgb(117, 163, 208)
            Next
            e.Row.Cells(1).Text = Me._total.ToString("#,##0.00")
            e.Row.Cells(2).Text = Me._prog.ToString("#,##0.00")
            e.Row.Cells(3).Text = Me._transf.ToString("#,##0.00")
            e.Row.Cells(4).Text = Me._girado.ToString("#,##0.00")
            If Me._prog = 0 Then
                e.Row.Cells(5).Text = (0).ToString("0 %")
            Else
                e.Row.Cells(5).Text = (Me._girado / Me._prog).ToString("0 %")
            End If
            e.Row.Cells(6).Text = ""
            e.Row.Cells(7).Text = ""
        ElseIf e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
        End If
    End Sub

    Protected Sub gvconsolidado_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvconsolidado.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' Capturamos TIPO ASIGNACION RECURSO de la fila
            Dim hfPPER As HiddenField = CType(e.Row.FindControl("hfPPER"), HiddenField)
            ' Capturamos su descripcion
            If hfPPER.Value = "1" Then
                ViewState("descrip") = "TRANSFERENCIA FINANCIERA"
            End If
            If hfPPER.Value = "2" Then
                ViewState("descrip") = "ASIGNACION DIRECTA (OTRAS ENTIDADES)"
            End If
            If hfPPER.Value = "3" Then
                ViewState("descrip") = "ASIGNACION DIRECTA (DEVIDA)"
            End If
            'Filtramos el detalle por TIPO ASIGNACION RECURSO
            Dim IDpper As String = hfPPER.Value.ToString
            Dim vst As DataView = New DataView(ViewState("Data2"), "TIPO_ASIGNACION_RECURSO='" + IDpper + "'", "", DataViewRowState.CurrentRows)
            Dim gdv As GridView = e.Row.Cells(0).FindControl("gvDetalle")
            e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Left
            e.Row.Cells(0).BackColor = Drawing.Color.White
            gdv.DataSource = vst
            gdv.DataBind()
        End If
    End Sub

End Class
